#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct stu
{
	int age;
	char name[10];
};
int cmp_by_name(const void* e1, const void* e2)
{
	return strcmp(((struct stu*)e1)->name, ((struct stu*)e2)->name);
}
int cmp_by_age(const void* e1, const void* e2)
{
	return ((struct stu*)e1)->age - ((struct stu*)e2)->age;
}
int cmp_by_int(const void* e1, const void* e2)
{
	return *(int*)e1 - *(int*)e2;
}
int cmp_by_char(const void* e1, const void* e2)
{
	return *(char*)e1 - *(char*)e2;
}
void test1()
{
	int arr[10] = { 3,1,7,2,4,6,9,0,8,5 };
	size_t sz = sizeof(arr) / sizeof(arr[0]);
	qsort(arr, sz, sizeof(arr[0]), cmp_by_int);
	int i = 0;
	for (i = 0; i<sz; i++)
	{
		printf("%d ", *(arr + i));
	}
}
void test2()
{
	char arr[3] = { 'a','z','k'};
	size_t sz = sizeof(arr)/sizeof(char);
	qsort(arr, sz, sizeof(arr[0]), cmp_by_char);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%c ", *(arr + i));
	}
}

void test3()
{
	struct stu s[] = { {18,"zhangsan"},{16,"lisi"},{20,"wangwu"} };
	size_t sz = sizeof(s) / sizeof(s[0]);
	qsort(s, sz, sizeof(s[0]), cmp_by_name);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s %d\n",s[i].name,s[i].age);
	}
}
void test4()
{
	struct stu s[] = { {18,"zhangsan"},{16,"lisi"},{20,"wangwu"} };
	size_t sz = sizeof(s) / sizeof(s[0]);
	qsort(s, sz, sizeof(s[0]), cmp_by_age);
	int i = 0;
	for (i = 0; i < sz; i++)
	{
		printf("%s %d\n", s[i].name, s[i].age);
	}
}
int main()
{
	test1();
	printf("\n");
	test2();
	printf("\n");
	test3();
	test4();
	return 0;
}